package com.ruoyi.system.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.TActivityInfo;
import com.ruoyi.system.domain.TPupilInfo;
import com.ruoyi.system.domain.TTeacherInfo;
import com.ruoyi.system.mapper.TActivityInfoMapper;
import com.ruoyi.system.mapper.TPupilInfoMapper;
import com.ruoyi.system.mapper.TTeacherInfoMapper;
import com.ruoyi.system.service.ICommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class CommonServiceImpl implements ICommonService {
    @Resource
    private TActivityInfoMapper tActivityInfoMapper;

    @Resource
    private TPupilInfoMapper tPupilInfoMapper;

    @Resource
    private TTeacherInfoMapper tTeacherInfoMapper;

    @Override
    public String gainThreeNoByNo(String type) {
//        LambdaQueryWrapper<TActivityInfo> queryWrapper = new LambdaQueryWrapper<>();
//        tActivityInfoMapper.selectList(queryWrapper);
        return type.concat(DateUtils.getDate()).concat(getMaxIdByType(type));
    }

    private String getMaxIdByType(String type) {
        String code = getCode(type);
        log.info("----- getMaxIdByType code:{}", code);
        if (code.length() == 1) {
            return "000".concat(code);
        } else if (code.length() == 2) {
            return "00".concat(code);
        } else if (code.length() == 3) {
            return "0".concat(code);
        }
        return code;
    }

    private String getCode (String type) {
        switch (type) {
            case "AT":
                TActivityInfo tActivityInfo = tActivityInfoMapper.queryActivityMaxId();
                return tActivityInfo == null ? "1" : String.valueOf(tActivityInfo.getId() + 1);
            case "P":
                TPupilInfo tPupilInfo = tPupilInfoMapper.queryPupilMaxId();
                return tPupilInfo == null ? "1" : String.valueOf(tPupilInfo.getId() + 1);
            case "T":
                TTeacherInfo tTeacherInfo = tTeacherInfoMapper.queryTeacherMaxId();
                return tTeacherInfo == null ? "1" : String.valueOf(tTeacherInfo.getId() + 1);
            default:
                return RandomUtil.randomNumbers(4);
        }
    }
}
